import { Author } from '../entity/Author';
import { AppDataSource } from "../data-source"
import { Album } from "../entity/Album"
import { Photo } from "../entity/Photo"

export const insertAlbumRelation = async () => {

  const album1 = new Album()
  album1.name = "Bears"
  await AppDataSource.manager.save(album1)

  const album2 = new Album()
  album2.name = "Me"
  await AppDataSource.manager.save(album2)

  const author = new Author()
  author.name = '刘德华'

  const photo = new Photo()
  photo.name = "Me and Bears"
  photo.description = "I am near polar bears"
  photo.filename = "photo-with-bears.jpg"
  photo.views = 1
  photo.author = author
  photo.isPublished = true
  photo.albums = [album1, album2]
  await AppDataSource.manager.save(photo)

  const loadedPhoto = await AppDataSource.getRepository(Photo).findOne({
    where: {
      id: 9,
    },
    relations: {
      albums: true,
    },
  })

  console.log('loadedPhoto :>> ', loadedPhoto);

}